#include<bits/stdc++.h>
#include "graph.h"
using namespace std;
#define ll long long
const int N=205;
int st[2*N], top;
int cd[N], bck[N][N];
int id[N][N];
int fa[N];
int tot;
void dfs1(){
	int x=++tot;
	int z=LastRoad();
	cd[x]=NumberOfRoads();
	for(int i=1; i<=cd[x]; ++i){
		if(i==z) continue;
		Move(i, 2);
		if(Color()==2){
			Move(LastRoad(), 2);
			bck[x][i]=1;
		}else if(Color()==1){
			st[++top]=i;
			fa[tot+1]=x;
			dfs1();
		}else{
			Move(LastRoad(), 3);
		}
	}
	if(z!=-1) Move(z, 3), st[++top]=0;
}
int tp;
void dfs2(int t){
	int x=++tot;
	int z=LastRoad();
//	assert(cd[x]==NumberOfRoads());
	for(int i=1; i<=cd[x]; ++i){
		if(bck[x][i]){
			Move(i, (x/t)%3+1);
			id[x][i]+=(Color()-1)*t;
			Move(LastRoad(), Color());
		}
	}
	while(tp<top){
		int o=st[++tp];
		if(!o){
			Move(z, (x/t)%3+1);
			break;
		}
		Move(o, (x/t)%3+1);
		dfs2(t);
	}
}
int d[N][N];
void Inspect(int R) {
	dfs1();
	for(int i=0, t=1; i<5; ++i, t*=3){
		tp=0, tot=0;
		dfs2(t);
	}
	memset(d, 0x3f, sizeof(d));
	for(int i=1; i<=tot; ++i){
		if(fa[i]) d[i][fa[i]]=1, d[fa[i]][i]=1;
		for(int j=1; j<=cd[i]; ++j){
			if(bck[i][j]){
				d[i][id[i][j]]=1;
				d[id[i][j]][i]=1;
			}
		}
	}
	for(int k=1; k<=tot; ++k){
		for(int i=1; i<=tot; ++i){
			for(int j=1; j<=tot; ++j){
				d[i][j]=min(d[i][j], d[i][k]+d[k][j]);
			}
		}
	}
	map<int,int> mp;
	for(int i=1; i<=tot; ++i){
		for(int j=i+1; j<=tot; ++j){
			mp[d[i][j]]++;
		}
	}
	for(int i=1; i<=R; ++i){
		Answer(i, mp[i]);
	}
}

//################################
/*
#include "graph.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_N 201
#define N_MOVE_MAX 1500000

static int selfN;
static int M;
static int X;
static int R;
static int n_roads[MAX_N];
static int road[MAX_N][MAX_N];
static int road_idx[MAX_N][MAX_N];

static int color[MAX_N];
static int pos;
static int last_road;

static int called_answer[MAX_N];
static int call_move;

static int expected_answer[MAX_N];

static void Wrong(int message) {
    printf("Wrong Answer [%d]\n", message);
    exit(1);
}

void Answer(int D, int A) {
    if (!(1 <= D && D <= R)) {
        Wrong(1);
    }
    if (called_answer[D] != -1) {
        Wrong(2);
    }

    called_answer[D] = A;
}

void Move(int I, int V) {
	fprintf(stderr, "Move %d %d [%d->%d]\n", I, V, pos, road[pos][I-1]);
    if (I < 1 || n_roads[pos] < I) {
        Wrong(5);
    }
    if (V < 1 || X < V) {
        Wrong(6);
    }

    ++call_move;

    if (call_move > N_MOVE_MAX) {
        Wrong(7);
    }
    --I;
    color[pos] = V;
    last_road = road_idx[road[pos][I]][pos] + 1;
    pos = road[pos][I];
}

int NumberOfRoads() {
    return n_roads[pos];
}

int LastRoad() {
    return last_road;
}

int Color() {
    return color[pos];
}

int main(void) {
    int i, j;
    if (scanf("%d%d%d", &selfN, &X, &R) != 3) {
        fprintf(stderr, "error: cannot read selfN, X and R.\n");
        exit(1);
    }

    if (selfN < 2 || MAX_N < selfN) {
        fprintf(stderr, "error: selfN is out of bounds.\n");
        exit(1);
    }

    for (i = 0; i < selfN; ++i) {
        scanf("%d", &(n_roads[i]));
		M+=n_roads[i];
        for (j = 0; j < selfN; ++j)
            road_idx[i][j] = -1;
        for (j = 0; j < n_roads[i]; ++j) {
            scanf("%d", &(road[i][j]));
            --road[i][j];
            road_idx[i][road[i][j]] = j;
        }
    }
	M/=2;
    for (i = 1; i <= R; ++i) {
        scanf("%d", &(expected_answer[i]));
    }

    for (i = 0; i < selfN; ++i) {
        color[i] = 1;
    }

    pos = 0;
    last_road = -1;
    call_move = 0;
    for (i = 1; i <= R; ++i)
        called_answer[i] = -1;

    Inspect(R);

    for (i = 1; i <= R; ++i) {
        if (called_answer[i] == -1) {
            Wrong(3);
        }
        if (called_answer[i] != expected_answer[i]) {
            Wrong(4);
        }
    }

    printf("Accepted: #move = %d\n", call_move);
	cerr << 1.0*call_move/M << '\n';
    return 0;
}
*/
